package com.pucheng.bracelet.app.ble

import android.text.TextUtils
import com.blankj.utilcode.util.LogUtils
import com.pucheng.bracelet.app.util.ByteUtils
import com.pucheng.bracelet.app.util.CacheUtil
import com.pucheng.bracelet.app.util.I18NUtils

object BleCmd {
    /**
     * 个人信息
     */
    val SYNC_USER_INFO: String = "FB13"

    /**
     * 设置表盘
     */
    val GET_CLOCK_DIAL: String? = "FB02"

    /**
     * 设置表盘
     */
    val SET_CLOCK_DIAL: String? = "FB03"

    /**
     * 设置闹钟
     */
    val SYNC_CLOCK: String? = "FB04"

    /**
     * 消息推送开关
     */
    val PUSH_STATE: String? = "FB15"

    /**
     * 同步语言
     */
    const val SYNC_LANGUAGE = "FB0101"

    /**
     * 同步语言
     */
    const val SYNC_TIME = "FB08"

    /**
     * FOTA
     */
    const val OPEN_FOTA = "FB1701"

    /**
     * 读取当前步数、卡路里、里程数据
     */
    const val GET_STEPS_INFO = "FB4001"

    /**
     * 设置目标步数
     */
    const val SET_STEPS_FLAG = "FB0D"

    /**
     * 读取历史步数、卡路里、里程数据
     */
    const val GET_HIS_STEPS_INFO = "FB4101"

    /**
     * 读取睡眠指令
     */
    const val GET_SLEEP_DATA = "FB4301"

    /**
     * 读取心率数据
     */
    const val GET_HR_DATA = "FB4401"

    /**
     * 读取心率数据
     */
    const val GET_HR_DATA_MONITOR = "FB4201"

    /**
     * 查找手表
     */
    const val FIND_WATCH = "FB06"

    /**
     * 测量血氧饱和度
     */
    const val MEASURED_BLOOD_OXYGEN = "FB0F01"

    /**
     * 测量血压
     */
    const val MEASURED_BP = "FB1101"

    /**
     * 停止血压
     */
    const val STOP_MEASURED = "FB12"

    /**
     * 测量心率
     */
    const val MEASURED_HR = "FB0E01"

    /**
     * 恢复出厂设置
     */
    const val RESTORE_SYS_SETTING = "FB0C"

    /**
     * 设置时间格式
     */
    const val SET_TIME_UNIT = "FB07%s"

    /**
     * 久坐提醒设置
     */
    const val SIT_FOR_A_LONG = "FB09%s"

    /**
     * 拍照
     */
    const val START_CAMERA = "FB05"

    /**
     * 天气
     */
    const val SYNC_WEATHER = "FB18"

    /**
     * 是否开启全天心率测量
     */
    const val OPEN_ALL_DAY_HR_MEASURE = "FB0A%s"

    fun getQuickViewCmd(isOpen: Boolean): String {
        val sb = StringBuffer("FB0B")
        val openState = if (isOpen) {
            "01"
        } else {
            "00"
        }
        sb.append(openState)
        var quickViewPeriod = CacheUtil.getQuickViewPeriod()
        val strStart = quickViewPeriod.split("-")[0].split(":")
        sb.append(ByteUtils.intToHexByLength(strStart[0].toInt(),1))
        sb.append(ByteUtils.intToHexByLength(strStart[1].toInt(),1))
        val strEnd = quickViewPeriod.split("-")[1].split(":")
        sb.append(ByteUtils.intToHexByLength(strEnd[0].toInt(),1))
        sb.append(ByteUtils.intToHexByLength(strEnd[1].toInt(),1))
//        quickViewPeriod.replace(":".toRegex(), "").replace("-".toRegex(), "").trim { it <= ' ' }
//        sb.append(quickViewPeriod)
//        sb.append("000000000000000000000000bb")
        return sb.toString()
    }

    fun getSyncTimeCmd(): String {
        val temp = ByteUtils.decToHex((System.currentTimeMillis().toString().substring(0,10)).toInt())
        LogUtils.e("temp:$temp")
        val syncTime = StringBuffer(temp)
        val gmtStr = I18NUtils.getCurrentTimeZone()

        LogUtils.e("gmtStr:$gmtStr")
        if (!TextUtils.isEmpty(gmtStr)) {
            if (gmtStr.contains("-")) {
                syncTime.append("00")
            } else if (gmtStr.contains("+")) {
                syncTime.append("01")
            }
            syncTime.append(gmtStr.substring(4,6))
        }

        return syncTime.toString()
    }
}